/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.usuallyinterface;

import com.lightkits.common.TransactionValidate;
import com.lightkits.common.db.entity.FndItemBase;
import com.lightkits.common.db.entity.FndOrganizationBase;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.common.utility.validate.IntegrityValidator;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.database.dao.WmsWarehouseMapper;
import com.lightkits.wms.database.model.ExceptionModel;
import com.lightkits.wms.dto.usuallyinterface.GetCommBadMatRequestType;
import com.lightkits.wms.dto.usuallyinterface.GetCommBadMatResponseType;
import com.lightkits.wms.dto.usuallyinterface.model.BadMatReasonDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/**
 * GetCommBadMatReaExecutor
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/5/17 11:16
 */
@Component
public class GetCommBadMatReaExecutor implements
        ServiceExecutor<GetCommBadMatRequestType, GetCommBadMatResponseType> {

    private final WmsWarehouseMapper wmsWarehouseMapper;

    @Autowired
    public GetCommBadMatReaExecutor(WmsWarehouseMapper wmsWarehouseMapper) {
        this.wmsWarehouseMapper = wmsWarehouseMapper;
    }

    @Override
    public GetCommBadMatResponseType service(GetCommBadMatRequestType request) {
        GetCommBadMatResponseType response = new GetCommBadMatResponseType();
        response.setSuccessful(true);
        try {

            //参数校验
            IntegrityValidator.begin().validate(request.getItemId(), "Item Id")
                    .validate(request.getOrgId(), "Organization Id");

            TransactionValidate check1 = TransactionValidate.beginValidity(FndItemBase.class);
            check1.checkIdValidity(ObjectUtil.toString(request.getItemId()), "ITEM_ID");

            check1 = TransactionValidate.beginValidity(FndOrganizationBase.class);
            check1.checkIdValidity(ObjectUtil.toString(request.getOrgId()), "ORGANIZATION_ID");

            //可以对每个时间域单独修改
            Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH) + 1;
            int date = c.get(Calendar.DATE);
            //获取近三个月的常用不良原因
            String startDate = year + "-" + (month - 3) + "-" + date + " 00:00:00";
            String endDate = year + "-" + month + "-" + date + " 00:00:00";
            List<ExceptionModel> models = wmsWarehouseMapper.getBadReasonByParams(
                    request.getOrgId(), request.getItemId(), startDate, endDate);
            List<BadMatReasonDTO> badMatReasonDTOS = new ArrayList<>();
            if (ObjectUtil.isNullOrEmpty(models)) {
                response.setBadMatReasonDTOS(badMatReasonDTOS);
                return response;
            }
            models.forEach(target -> {
                BadMatReasonDTO dto = new BadMatReasonDTO();
                dto.setExceptionId(target.getExceptionId());
                dto.setExceptionCode(target.getExceptionCode());
                dto.setExceptionName(target.getExceptionName());
                dto.setExceptionAlias(target.getExceptionAlias());
                badMatReasonDTOS.add(dto);
            });
            response.setBadMatReasonDTOS(badMatReasonDTOS);
        } catch (Exception e) {
            response.setSuccessful(false);
            response.setMessage(e.getMessage());
        }
        return response;
    }
}
